from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils.crypto import get_random_string
from django.conf import settings

from .models import Comment


@receiver(pre_save, sender=Comment)
def create_comment_slug(sender, instance, *args, **kwargs):
    """在评论创建之前，自动生成短标题(slug)"""
    if not instance.slug:
        instance.slug = get_random_string(settings.SLUG_SIZE)
